package com.kjj.car.service.impl;

import com.kjj.basic.constants.BaseConstants;
import com.kjj.basic.service.impl.BaseServiceImpl;
import com.kjj.car.domain.CarType;
import com.kjj.car.mapper.CarTypeMapper;
import com.kjj.car.service.ICarTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author kan
 * @since 2022-09-05
 */
//@Service
public class CarTypeServiceImpl_springCache extends BaseServiceImpl<CarType> implements ICarTypeService {

    @Autowired
    private CarTypeMapper carTypeMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     *  循环算法
     * @param pid
     * @return
     */
    public List<CarType> getCarTypesByLoop(Long pid) {
        List<CarType> result = new ArrayList<>();
        List<CarType> allCarTypes = carTypeMapper.loadAll();
        Map<Long, CarType> allCarTypeMap = new HashMap<>();
        //建立map映射关系  方案二
        for (CarType carType : allCarTypes) {
            allCarTypeMap.put(carType.getId(), carType);
        }
        for (CarType carType : allCarTypes) {
            Long pidTmp = carType.getPid();
            if (pidTmp.longValue()==pid.longValue()){
                result.add(carType);
            }else {
                //获取到父亲
//                CarType parent = null;
                //方案一
//                for (CarType type : allCarTypes) {
//                    if (type.getId().longValue()==pidTmp.longValue()){
//                        parent = type;
//                        break;
//                    }
//                }
                CarType parent = allCarTypeMap.get(pidTmp);
                //当做父亲的儿子  放入父亲的集合
                parent.getChildren().add(carType);
            }
        }
        return result;
    }

    public List<CarType> getCarTypesByRecursion(Long pid) {
        List<CarType> children = carTypeMapper.loadChildrenByPid(pid);
        if (children == null) {
            return null;
        }
        for (CarType child : children) {
            List<CarType> grandson = tree(child.getId());
            child.setChildren(grandson);
        }
        return children;
    }
    // 注解版获取类型树
    @Cacheable(cacheNames = BaseConstants.CAR_TYPE_TREE_IN_REDIS,key = "'ALL'")
    @Override
    public List<CarType> tree(Long pid) {
        return getCarTypesByLoop(pid);
    }

    @CacheEvict(cacheNames = BaseConstants.CAR_TYPE_TREE_IN_REDIS,key = "'ALL'")
    @Override
    public void add(CarType carType) {
        super.add(carType);
    }

    @CacheEvict(cacheNames = BaseConstants.CAR_TYPE_TREE_IN_REDIS,key = "'ALL'")
    @Override
    public void del(Serializable id) {
        super.del(id);
    }
    @CacheEvict(cacheNames = BaseConstants.CAR_TYPE_TREE_IN_REDIS,key = "'ALL'")
    @Override
    public void update(CarType carType) {
        super.update(carType);
    }
}
